﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RecipeTracker
{
    /// <summary>
    /// The recipe class contains the properties of recipes. Just a simple class with read-write
    /// properties
    /// </summary>
    public class Recipe : IComparable
    {
        /// <summary>
        /// The list of Ingredients that are required for this recipe
        /// </summary>
        private List<Ingredient> _ingredients;
        public List<Ingredient> Ingredients
        {
            get
            {
                if (_ingredients == null)
                {
                    _ingredients = new List<Ingredient>();
                    return _ingredients;
                }
                else
                    return _ingredients;
            }
            set { _ingredients = value; }
        }

        private List<Step> mSteps;
        public List<Step> Steps
        {
            get
            {
                if (mSteps == null)
                {
                    mSteps = new List<Step>();
                    return mSteps;
                }
                else
                    return mSteps;
            }
            set { mSteps = value; }
        }

        public Category RecipeCategory
        {
            get;
            set;
        }

        public int CategoryId
        {
            get;
            set;
        }

        public int RecipeId
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public int Temperature
        {
            get;
            set;
        }

        public string BakingTime
        {
            get;
            set;
        }

        public int StepIdentity
        {
            get;
            set;
        }

        public Recipe()
        {

        }

        public override string ToString()
        {
            StringBuilder tmp = new StringBuilder();
            RecipeCategory = Category.CategoryLookup[CategoryId];
            tmp.Append("Name: ");
            tmp.Append(Name);
            tmp.Append("\r\n");
            tmp.Append(RecipeCategory.ToString());
            tmp.Append("\r\n");
            tmp.Append("Cook Time: ");
            tmp.Append(BakingTime);
            tmp.Append("\r\n");
            tmp.Append("Temperature: ");
            tmp.Append(Temperature);
            tmp.Append("\r\n\r\n");
            tmp.Append("Description\r\n");
            List<string> lines = splitDescriptionToLines();
            foreach (string s in lines)
            {
                tmp.Append(s);
                tmp.Append("\r\n");
            }
            tmp.Append("\r\n\r\n");
            tmp.Append("Ingredients\r\n");
            foreach (Ingredient i in Ingredients)
            {
                tmp.Append(i.ToString());
                tmp.Append("\r\n");
            }
            tmp.Append("\r\n");
            tmp.Append("Steps\r\n");
            foreach (Step s in Steps)
            {
                tmp.Append(s.ToString());
                tmp.Append("\r\n");
            }
            return tmp.ToString();
        }

        private List<string> splitDescriptionToLines()
        {
            //split the description to lines
            List<string> lines = new List<string>();
            string[] lines2 = Description.Split(' ');
            StringBuilder s = new StringBuilder();

            foreach (string str in lines2)
            {
                s.Append(str);
                s.Append(" ");
                if (s.Length > 65)
                {
                    lines.Add(s.ToString());
                    s = new StringBuilder();
                }
            }

            lines.Add(s.ToString());
            return lines;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if (obj is Recipe)
            {
                Recipe in2 = (Recipe)obj;
                return Name.CompareTo(in2.Name);
            }
            else
                throw new ArgumentException("obj is not a valid Ingredient");
        }

        #endregion
    }
}
